<?php 
date_default_timezone_set("Asia/Shanghai");

/*
for($i=0;$i<100;$i++){
	echo md5(uniqid(md5(microtime(true)),true))  . "<br>";
}
*/

/*
//Token密钥
global $secret;
$secret = "wwwclxfcom!@#sellgame123";


if(empty($_GET['token']))
{
	$code = '101';	//登录信息不完整
	
}
else
{
	$checks = jwt_decode($_GET['token'], $secret);
	var_dump( $checks["payload"] );
}

*/

//jwt加密
function jwt_encode($uinfo, $secret){
	$header_arr = array(
	'typ' => 'JWT',		//token类型
	'alg' => 'HS256'	//加密方法
	);
	$payload_arr = array(
		'iss' => 'clxf.com',	//发行方
		'exp' => '2592000',		//有效期30天
		'uinfo' => $uinfo,		//用户信息
		'pubtime' => time()		//发布时间
	);
	$header = base64_encode(json_encode($header_arr));
	$payload = base64_encode(json_encode($payload_arr));
	$signature = base64_encode(hash_hmac('sha256', $header . "." . $payload, $secret, FALSE));
	$tokens = $header . "." . $payload . "." . $signature;
	return $tokens;
}

//jwt解密
function jwt_decode($tokens, $secret){
	$arr = explode(".",$tokens);
	$header = $arr[0];
	$payload = $arr[1];
	$signature = base64_encode(hash_hmac('sha256', $header . "." . $payload, $secret, FALSE));
	if($signature==$arr[2]){
		$data["header"] = json_decode(base64_decode($header));
		$data["payload"] = json_decode(base64_decode($payload));

		return $data;
		/*
		if( (time()-$data["payload"]["pubtime"])>$data["payload"]["exp"] ){
			return "c102";	//登录超期
		} else {
			return $data;
		}
		*/	
	}
	else{
		return "c101";	//内容被更改，错误
	}
}

/*
$arr['a'] = 'aaa';
$arr['b'] = 'bbb';
$arr['c'] = 'ccc';

$res1 = json_encode($arr);
$res2 = json_decode($res1, true);
var_dump($res1);
echo '<br>';
var_dump($res2);

for($i=0;$i<100;$i++){
	$a = mt_rand(1,100000);
	echo $a . "<br />";
}
*/

echo strtotime("20171131");
?>